package com.renge.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.renge.commonutils.R;
import com.renge.eduservice.entity.EduCourse;
import com.renge.eduservice.entity.json.CourseInfoJson;
import com.renge.eduservice.entity.json.CoursePublishJson;
import com.renge.eduservice.entity.query.CourseQuery;
import com.renge.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-04
 */
@Api(description="课程管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;
    @ApiOperation(value = "添加或者更新课程基本信息")
    @PostMapping("/add")
    public R addCourse(@ApiParam(name = "courseInfoJson",value = "课程基本信息",required = true)
                            @RequestBody CourseInfoJson courseInfoJson){
        String id = eduCourseService.add(courseInfoJson);
        return R.ok().data("courseid",id);
    }
    @ApiOperation(value = "根据id查询返回courseInfoJson")
    @GetMapping("/{courseId}")
    public R getCourseInfoJsonById(@ApiParam(name = "courseId",value = "课程的id",required = true)
                      @PathVariable("courseId") String id){
        CourseInfoJson courseInfoJson=eduCourseService.getCourseInfoJsonById(id);
        if(courseInfoJson==null){
            return R.error();
        }
        return R.ok().data("courseInfoJson",courseInfoJson);
    }

    @ApiOperation(value = "根据id查询返回CoursePublishJson")
    @GetMapping("public/{courseId}")
    public R getCoursePublishJsonById(@ApiParam(name = "courseId",value = "课程的id",required = true)
                                   @PathVariable("courseId") String id){
        CoursePublishJson coursePublishJson=eduCourseService.getCoursePublishJsonById(id);
        if(coursePublishJson==null){
            return R.error();
        }
        return R.ok().data("courseInfoJson",coursePublishJson);
    }

    @ApiOperation(value = "添加或者更新课程基本信息")
    @PostMapping("/public/change")
    public R addPublic(@ApiParam(name = "EduCourse",value = "课程发布状态更改: Draft未发布  Normal已发布",required = true)
                       @RequestBody EduCourse eduCourse){
        EduCourse byId = eduCourseService.getById(eduCourse.getId());
        byId.setStatus(eduCourse.getStatus());
        boolean b = eduCourseService.updateById(byId);
        return b?R.ok():R.error();
    }
    @ApiOperation(value = "分页查询eduCourse")
    @PostMapping("{current}/{pagesize}")
    public R listPageByQuery(@ApiParam(name = "current",value = "当前页",required = true)
                                 @PathVariable("current")Integer current,
                             @ApiParam(name = "pagesize",value = "数据数量",required = true)
                                 @PathVariable("pagesize")Integer pagesize,
                             @ApiParam(name = "CourseQuery",value = "课程查询条件",required = false)
                                 @RequestBody CourseQuery CourseQuery){
        Page<EduCourse> page = new Page<>(current,pagesize);
        QueryWrapper<EduCourse> Wrapper = new QueryWrapper<>();
        //按照时间排序
        Wrapper.orderByDesc("gmt_create");
        //调用逻辑
        IPage<EduCourse> eduCourseIPage = eduCourseService.pageCourseList(page, Wrapper, CourseQuery);
        //组装data
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total",eduCourseIPage.getTotal());
        hashMap.put("rows",eduCourseIPage.getRecords());
        return R.ok().data(hashMap);
    }
    @ApiOperation(value = "根据id删除课程")
    @DeleteMapping("{courseId}")
    public R deleteById(@ApiParam(name = "courseId" ,value = "课程id",required = true)
                        @PathVariable("courseId")String courseId){
        eduCourseService.deleteById(courseId);
        return R.ok();
    }

}

